<template>
	<BaseContainer>
		<aplid-base-crud ref="crud" class="w-100" v-bind="{ ...theConfig, ...$attrs }" :beforeOnLoadFormate="beforeOnLoadFormate">
			<!-- <template v-slot:menuRight>
        <MessageList type="正在采集的数据" :dataType="3"></MessageList>
      </template> -->
			<template v-slot:menuColumn="{ row }">
				<el-button v-if="row.dataStatus && row.dataStatus !== '0'" @click="handleChange(row)" text type="warning"> 变更中 </el-button>
			</template>
			<template v-slot:addDialog="{ row, show }">
				<TabForm @refresh="handleRefresh" @operate="handleOperate" :api="baseConfig.api" :key="show" :formConfig="formConfig" :nowRowData="row">
				</TabForm>
			</template>

			<template v-slot:detailDialog="{ row, show }">
				<Archives
					@refresh="handleRefresh"
					@refreshPage="handleRefreshPage"
					@rechange="handleOperate"
					:api="baseConfig.api"
					:detailConfig="baseConfig.detailConfig"
					:key="show"
					:nowRowData="row"
				>
				</Archives>
			</template>
			<template v-slot:importDialog="{ row, show }">
				<ImportForm
					@refresh="handleRefresh"
					:importModeTitle="importModeTitle"
					:paramsObj="paramsObj"
					:api="baseConfig.api"
					:key="show"
					:nowRowData="row"
				></ImportForm>
			</template>
			<template v-slot:RechangeDialog="{ row, show }">
				<Rechange @refresh="handleRefresh" :api="baseConfig.api" :key="show" :nowRowData="row"></Rechange>
			</template>
		</aplid-base-crud>
		<el-dialog v-model="dialogVisible" title="数据变更" width="65%">
			<RechangeDetail v-if="dialogVisible" @refresh="handleRefresh" :batchNumber="nowData.dataStatus"></RechangeDetail>
		</el-dialog>
	</BaseContainer>
</template>

<script setup lang="ts">
import { baseConfig, beforeSearchQuery } from './config'
import TabForm from './components/TabForm/index.vue'
import Archives from './components/Archives/index.vue'
import ImportForm from './components/ImportForm/index.vue'
import Rechange from './components/Rechange/index.vue'
import MessageList from '@/views/aplid/MessageList/index.vue'
import RechangeDetail from '@/views/datacenter/components/DataGather/change/detail.vue'
import { editConfig } from '@/views/datacenter/comprehensiveQueries/utils/index'
import { cloneDeep } from 'lodash-es'

type Props = {
	isComprehensiveQueries?: boolean
	formConfig?: any
	paramsObj?: any
	importModeTitle?: string
}
let props = withDefaults(defineProps<Props>(), {
	isComprehensiveQueries: false,
})
let theConfig: any = ref({})
if (baseConfig) {
	let defaultConfig = cloneDeep(baseConfig)
	if (props.isComprehensiveQueries) {
		theConfig.value = editConfig(defaultConfig)
	} else {
		theConfig.value = baseConfig
	}
}
const crud = ref()
const dialogVisible = ref(false)
let nowData: any = {}
//  处理刷新
const handleRefresh = (query) => {
	dialogVisible.value = false
	nextTick(() => {
		crud.value.refreshChange(query)
	})
}
const handleOperate = (type, row, title) => {
	nextTick(() => {
		crud.value.handleOperate(type, row, title)
	})
}
const handleRefreshPage = () => {
	nextTick(() => {
		crud.value.refreshChange({}, true)
	})
}
const handleChange = (row) => {
	nowData = row
	dialogVisible.value = true
}

const beforeOnLoadFormate = async (params,searchMode) => {
	const query = beforeSearchQuery(params)
	const year = new Date().getFullYear()
	const betweenTime = `${year}-01-01,`
	const nameObj = {
		社区老年人认知障碍照护服务中心: 'nurseConstructionDateBetween',
		助餐点: 'constructionDateBetween',
		睦邻点: 'neighborlyConstructionDateBetween',
	}
	if (props.importModeTitle) {
		query[nameObj[props.importModeTitle]] = betweenTime
	}
  if(!searchMode?.soleValue) {
    query.soleValue = undefined
  }
	return Promise.resolve({
		...query,
	})
}

defineExpose({
	handleRefresh,
})
</script>

<style scoped></style>
